<?

require_once("MySmarty.php");
require_once("common.inc.php");
//require_once("ranking.inc.php");

$where = "";
$s = "";
$timediff = "";




if(isset($_GET['tag_id']) && is_numeric($_GET['tag_id'])){
	$tag_id = $_GET['tag_id'];
	$where .= " AND C.tag_id=$tag_id";	
	
}else if(isset($_GET['category_id']) && is_numeric($_GET['category_id'])){
	$category_id = $_GET['category_id'];
	//$sql = "SELECT category_id FROM category_mst WHERE pid=$category_id";
	
	$where .= " AND C.tag_id IN (SELECT tag_id FROM tag_mst WHERE category_id IN (SELECT category_id FROM category_mst WHERE pid=$category_id)) ";

}




if(isset($_GET['page']) && is_numeric($_GET['page'])){
	$page = $_GET['page'];
}else{
	$page = 1;
}

if(isset($_GET['page2']) && is_numeric($_GET['page2'])){
	$page2 = $_GET['page2'];
}else{
	$page2 = 1;
}


$perPage = 20;
$limit = "LIMIT ".($page-1)*$perPage.",$perPage";


$perPage2 = 20;
$limit2 = "LIMIT ".($page2-1)*$perPage2.",$perPage2";



if(isset($_GET['sort']) && is_numeric($_GET['sort'])){
	$sort = "ORDER BY A.view_cnt DESC";
	//$sort_link = "[<a href=\"index.php?page=$page\">新着順</a>][人気順]";
}else{
	$sort = "ORDER BY A.up_date DESC";
	//$sort_link = "[新着順][<a href=\"index.php&page=$page&sort=1\">人気順</a>]";
}

if(isset($_GET['sort2']) && is_numeric($_GET['sort2'])){
	$sort2 = "ORDER BY A.view_cnt DESC";
	//$sort_link = "[<a href=\"index.php?page=$page\">新着順</a>][人気順]";
}else{
	$sort2 = "ORDER BY A.up_date DESC";
	//$sort_link = "[新着順][<a href=\"index.php&page=$page&sort=1\">人気順</a>]";
}


$stamp = time();
$date = date('Y-m-d H:m:d', $stamp + 16*60*60);

$o_smarty = new MySmarty();


//$o_smarty->cache_lifetime = 60*60*1;//120分周期
$o_smarty->cache_lifetime = 60*60*3;

//キャッシュ
$key = hash('md5',$_SERVER['SCRIPT_NAME']."?".$_SERVER['QUERY_STRING']);


//if(!$o_smarty->is_cached('index.tpl',$key)){
	$con = $o_smarty->dbconnect();




	$site_arr = array();
	$sql = "SELECT * FROM site_mst";
	$result_site = $o_smarty->doSQL($sql);
	while($row_site = mysql_fetch_array($result_site)){
	
		$site_id = $row_site['site_id'];
		$site_arr['site_name'][$site_id] = $row_site['site_name'];
		$site_arr['reg_url'][$site_id] = $row_site['reg_url'];
		$site_arr['reg_thumb'][$site_id] = $row_site['reg_thumb'];	
		$site_arr['reg_flashobj'][$site_id] = $row_site['reg_flashobj'];	
	}	
	
	

echo "cached";
	
	$sql = "SET NAMES utf8";
	$o_smarty->doSQL($sql);
	
		
	$sql = "SELECT A.entry_id,A.title,A.entry_url,date_format(A.up_date, \"%m月%d日 %k:%i\") as up_date,A.urllist_id,B.blog_title,B.blog_url,A.site_id 
			FROM entrylist A 
			LEFT JOIN urllist B ON A.urllist_id = B.urllist_id
			LEFT JOIN tag_list C ON A.entry_id = C.entry_id			
			WHERE A.v_cnt > 0  AND A.site_id=1 $where
			$sort $limit";
			

	$sql2 = "SELECT A.entry_id,A.title,A.entry_url,date_format(A.up_date, \"%m月%d日 %k:%i\") as up_date,A.urllist_id,B.blog_title,B.blog_url,A.site_id 
			FROM entrylist A 
			LEFT JOIN urllist B ON A.urllist_id = B.urllist_id
			LEFT JOIN tag_list C ON A.entry_id = C.entry_id			
			WHERE A.v_cnt > 0  AND A.site_id IS NULL $where
			$sort2 $limit2";



	$sql_cnt = "SELECT count(*) as cnt FROM entrylist A 
				LEFT JOIN urllist B ON A.urllist_id = B.urllist_id 
			LEFT JOIN tag_list C ON A.entry_id = C.entry_id			
				WHERE A.v_cnt > 0 AND   A.site_id=1  $where";


	$sql_cnt2 = "SELECT count(*) as cnt FROM entrylist A 
				LEFT JOIN urllist B ON A.urllist_id = B.urllist_id 
			LEFT JOIN tag_list C ON A.entry_id = C.entry_id							
				WHERE A.v_cnt > 0 AND   A.site_id IS NULL  $where";				
	//echo $sql_cnt;
	
	$result = $o_smarty->doSQL($sql);
	$result_cnt = $o_smarty->doSQL($sql_cnt);
	if($s=='')$row_cnt = mysql_fetch_array($result_cnt);
	if($s=='')$total = $row_cnt['cnt'];
	//echo $sql;

	$result2 = $o_smarty->doSQL($sql2);
	$result_cnt2 = $o_smarty->doSQL($sql_cnt2);
	if($s=='')$row_cnt2 = mysql_fetch_array($result_cnt2);
	if($s=='')$total2 = $row_cnt2['cnt'];

	
	$sql = "SELECT count(*) as cnt FROM videolist WHERE open_flag=1 ";
	$result_all = $o_smarty->doSQL($sql);
	$row_all = mysql_fetch_array($result_all);
	$total_v = $row_all['cnt'];
	
	
	$i = 0;
	
	if($result){
		while($row = mysql_fetch_array($result)){
		
	
			
			$sql = "SELECT * FROM videolist WHERE entry_id=".$row['entry_id']." ORDER BY site_id ASC,cid DESC LIMIT 0,1";
			$resultv = doSQL($sql);
			
			
			if(mysql_num_rows($resultv)>0){
	
				$entry[$i]['title'] = $row['title'];
				$entry[$i]['title_disp'] = $row['title']!='' ? substr($row['title'],0,30)."..." : "無題";
				$entry[$i]['entry_id'] = $row['entry_id'];
				$entry[$i]['entry_url'] = $row['entry_url'];
				$entry[$i]['comment'] = $row['comment'];
				$entry[$i]['description'] = $row['description'];
				$entry[$i]['up_date'] = $row['up_date'];
				$entry[$i]['urllist_id'] = $row['urllist_id'];
				$entry[$i]['blog_title'] = $row['blog_title'] == "" ? "No Title" : $row['blog_title'];
				$entry[$i]['blog_url'] = $row['blog_url'];
				$entry[$i]['timediff'] = $row['timediff'];
				$entry[$i]['site_id'] = $row['site_id'];
				$tmp = $row['site_id'];
				$entry[$i]['site_name'] = $site_name[$tmp];
			
				$entry[$i]['v_cnt'] = mysql_num_rows($resultv);
				
				$rowv = mysql_fetch_array($resultv);
				$cid = $rowv['cid'];
				$cid2 = $rowv['cid2'];
				$time = $rowv['time'];
				$view_cnt_y = $rowv['view_cnt_y'];
				
				
				if($site_id==1){
					$entry[$i]['time_disp'] = gmdate('H:i:s', $time);
					$entry[$i]['view_cnt'] = $view_cnt_y;
				}
								
				$f = $rowv['filename'];
				$prefix = $prefix = substr($cid,0,2);
				$site_id= $entry[$i]['site_id'];

				if($cid!='' && $site_arr['reg_thumb'][$site_id]!=''){					
					$thumbnail_b = $site_arr['reg_thumb'][$site_id];

					//$thumbnail_b = strtr($thumbnail_b, 'img', $cid);
					$thumbnail_b = str_replace("{{cid}}", $cid, $thumbnail_b);
					$thumbnail_b = str_replace("{{cid2}}", $cid2, $thumbnail_b);

					if($site_id==13){
						$prefix = substr($cid,0,2);
						$thumbnail_b = str_replace("{{pre}}", $prefix, $thumbnail_b);
					}
					$entry[$i]['thumbnail'] = $thumbnail_b;
					$thumbnail_b = "";
				}


				$entry[$i]['cid'] = $cid;
				$entry[$i]['f'] = $f;
				$entry[$i]['icon'] = $site_icon[$site_id];
				
				//関連タグ
				$sql = "SELECT * FROM tag_list LEFT JOIN tag_mst ON  tag_list.tag_id =tag_mst.tag_id WHERE entry_id=".$row['entry_id']." LIMIT 0,3";
				$result_tag = doSQL($sql);
				if(mysql_num_rows($result_tag)!=0){
					$j=0;
					while($row_tag = mysql_fetch_array($result_tag)){
						$tag_name = "tag_name".$j;
						$tag_id = "tag_id".$j;
						$entry[$i][$tag_name] = $row_tag['tag_name'];
						$entry[$i][$tag_id] = $row_tag['tag_id'];
						$j++;
					}
				}
				
			}else{
				continue;		
			}
			$i++;	
			
		}
	}
	
	$i=0;
	if($result2){
		while($row = mysql_fetch_array($result2)){
		
	
			
			$sql = "SELECT * FROM videolist WHERE entry_id=".$row['entry_id']." ORDER BY site_id ASC,cid DESC LIMIT 0,1";
			$resultv = doSQL($sql);
			
			
			if(mysql_num_rows($resultv)>0){
	
				$entry2[$i]['title'] = $row['title'];
				$entry2[$i]['title_disp'] = $row['title']!='' ? $row['title'] : "無題";
				$entry2[$i]['entry_id'] = $row['entry_id'];
				$entry2[$i]['entry_url'] = $row['entry_url'];
				$entry2[$i]['comment'] = $row['comment'];
				$entry2[$i]['description'] = $row['description'];
				$entry2[$i]['up_date'] = $row['up_date'];
				$entry2[$i]['urllist_id'] = $row['urllist_id'];
				$entry2[$i]['blog_title'] = $row['blog_title'] == "" ? "No Title" : $row['blog_title'];
				$entry2[$i]['blog_url'] = $row['blog_url'];
				$entry2[$i]['timediff'] = $row['timediff'];
				$entry2[$i]['site_id'] = $row['site_id'];
				$tmp = $row['site_id'];
				$entry2[$i]['site_name'] = $site_name[$tmp];
			
				$entry2[$i]['v_cnt'] = mysql_num_rows($resultv);
				
				$rowv = mysql_fetch_array($resultv);
				$cid = $rowv['cid'];
				$cid2 = $rowv['cid2'];
				$time = $rowv['time'];
				$view_cnt_y = $rowv['view_cnt_y'];
				
				
				if($site_id==1){
					$entry2[$i]['time_disp'] = gmdate('H:i:s', $time);
					$entry2[$i]['view_cnt'] = $view_cnt_y;
				}
								
				$f = $rowv['filename'];
				$prefix = $prefix = substr($cid,0,2);
				$site_id= $entry2[$i]['site_id'];

				if($cid!='' && $site_arr['reg_thumb'][$site_id]!=''){					
					$thumbnail_b = $site_arr['reg_thumb'][$site_id];

					//$thumbnail_b = strtr($thumbnail_b, 'img', $cid);
					$thumbnail_b = str_replace("{{cid}}", $cid, $thumbnail_b);
					$thumbnail_b = str_replace("{{cid2}}", $cid2, $thumbnail_b);

					if($site_id==13){
						$prefix = substr($cid,0,2);
						$thumbnail_b = str_replace("{{pre}}", $prefix, $thumbnail_b);
					}
					$entry2[$i]['thumbnail'] = $thumbnail_b;
					$thumbnail_b = "";
				}


				$entry2[$i]['cid'] = $cid;
				$entry2[$i]['f'] = $f;
				$entry2[$i]['icon'] = $site_icon[$site_id];
				
				//関連タグ
				$sql = "SELECT * FROM tag_list LEFT JOIN tag_mst ON  tag_list.tag_id =tag_mst.tag_id WHERE entry_id=".$row['entry_id']." LIMIT 0,3";
				$result_tag = doSQL($sql);
				if(mysql_num_rows($result_tag)!=0){
					$j=0;
					while($row_tag = mysql_fetch_array($result_tag)){
						$tag_name = "tag_name".$j;
						$tag_id = "tag_id".$j;
						$entry2[$i][$tag_name] = $row_tag['tag_name'];
						$entry2[$i][$tag_id] = $row_tag['tag_id'];
						$j++;
					}
				}
				
			}else{
				continue;		
			}
			$i++;	
			
		}
	}
	
	
	//echo $sql_viewranking;
	//$o_smarty->dbclose($con);
	$stitle = urlencode("Youfilehostアダルト動画ナビ -AV女優別/ジャンル別に一発検索-");
	
	//$o_smarty->assign("actress_ranking",$actress_ranking);
	$tag_query = $_GET['sort']==1 ? "&sort=1" : "";
	//echo $tag_query;	
	$tag_query .= $_GET['site_id']!=0 && is_numeric($_GET['site_id']) ? "&site_id=".$_GET['site_id'] : "";
	$tag_query .= is_numeric($_GET['category_id']) ? "&category_id=".$_GET['category_id'] : "";
	$tag_query .= is_numeric($_GET['tag_id']) ? "&tag_id=".$_GET['tag_id'] : "";	


	//echo " ";
	if($_GET['sort']!=1){
		$headline = $page==1 ? "新着順　( ".$entry[0]['up_date']."更新 )" : "新着順";
	}else{
		$headline = "人気順";
	}

	$tag_query2 = $_GET['sort2']==1 ? "&sort2=1" : "";
	$tag_query2 .= $_GET['site_id']!=0 && is_numeric($_GET['site_id']) ? "&site_id=".$_GET['site_id'] : "";
	$tag_query2 .= is_numeric($_GET['category_id']) ? "&category_id=".$_GET['category_id'] : "";
	$tag_query2 .= is_numeric($_GET['tag_id']) ? "&tag_id=".$_GET['tag_id'] : "";		
	
	
	
	if($_GET['sort2']!=1){
		//echo "hoge:".print_r($entry2);
		$headline2 = $page2==1 ? "新着順　( ".$entry2[0]['up_date']."更新 )" : "新着順";
	}else{
		$headline2 = "人気順";
	}


	//動画サイト種別
	$sql_site = "select * from site_mst where open_flag=1";
	$result_site = doSQL($sql_site);
	$i = 0;
	while($rows = mysql_fetch_array($result_site)){
		$site[$i]['site_id'] = $rows['site_id'];
		$site[$i]['site_name'] = $rows['site_name'];
		$site[$i]['site_url'] = $rows['site_url'];
		if($_GET['site_id']==$rows['site_id']) $selected[$i]="selected"; else $selected[$i]="";
		$i++;
	}
	











/*************************************************************************************/
/*カテゴリーツリー(ここから)
/*************************************************************************************/


	$CatListArray = fetchTree($lang_id,$category_id);
	$keywordRstArray = fetchKeywordList($lang_id);
	
	
	$keyWordArray = getKeywordList($keywordRstArray);
	$html_str = "";
	$html_str = getCatList($CatListArray,1);
	//print_r($keyWordArray);
	

/*************************************************************************************/
/*カテゴリーツリー(ここまで)
/*************************************************************************************/
	
	//$o_smarty->dbclose($con);
	if(!is_numeric($page) || !is_numeric($total) 
		|| empty($actress_ranking) || empty($genre_ranking) || empty($entry) || empty($date) || empty($headline)){ 
		$o_smarty->caching = 0;
		$err_msg = "アクセス集中の為、正しく表示されない可能性があります。<br/>表示がおかしな場合は、お手数ですが再読み込みを行ってください。m(_ _)m";
	}


	$o_smarty->assign("err_msg",$err_msg);
	$o_smarty->assign("navi",getNavigation("category.php",$page,$total,$perPage,$tag_query));
	$o_smarty->assign("navi2",getNavigation2("category.php",$page2,$total2,$perPage,$tag_query2));
		
	$o_smarty->assign("genre_ranking",$genre_ranking);
	$o_smarty->assign("rankg",$rankg);
	$o_smarty->assign("site",$site);
	$o_smarty->assign("selected",$selected);

	$o_smarty->assign("entry2",$entry2);	
	$o_smarty->assign("entry",$entry);
	$o_smarty->assign("date",$date);
	
	$o_smarty->assign("sort",$_GET['sort']);
	$o_smarty->assign("sort2",$_GET['sort2']);
		
	$o_smarty->assign("headline",$headline);
	$o_smarty->assign("headline2",$headline2);
	$o_smarty->assign("tag_query",$tag_query);
		
	
	$o_smarty->assign("total_v",$total_v);
	$o_smarty->assign("site_name_all",$site_name_all);	
	$o_smarty->assign("tree_str",$html_str);	
	
//}

	
$o_smarty->display("category.tpl",$key);


?>